﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Xml;
using KoPortal.Sys;
using KoPortal2;

namespace WebServiceTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Useing - SharePoint Web Service
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            BasicHttpBinding binding = new BasicHttpBinding();
            binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;

            EndpointAddress endpoint =
                new EndpointAddress(@"http://sp.korablik.local/_vti_bin/KoServices/KoServices.SVC");

            KoReference.KoServicesClient Ko =
                new KoReference.KoServicesClient(binding, endpoint);

            Ko.ClientCredentials.Windows.AllowedImpersonationLevel =
                System.Security.Principal.TokenImpersonationLevel.Impersonation;

            //string webUrlStr = Ko.GetCurentWebUrl();
            //MessageBox.Show(webUrlStr);
            byte [] b = Ko.GetMySqlSrvList();
            string s = "";
            if (b != null)
            {
                DataTable newTable = new DataTable("tbl");
                using (System.IO.MemoryStream xmlStream = new System.IO.MemoryStream())
                {
                    xmlStream.Write(b, 0, b.Length);
                    xmlStream.Position = 0;
                    newTable.ReadXml(xmlStream);
                    dg.DataSource = newTable;
                }
            }

        }
        /// <summary>
        /// Simple Hello Worlds Service
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btbTest_Click(object sender, EventArgs e)
        {
            BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;

            EndpointAddress endpoint =
                new EndpointAddress(@"http://sp.korablik.local:85/WebServiceSys.asmx");

            KoReferenceOldPortal.WebServiceSysSoapClient ko =
                new KoReferenceOldPortal.WebServiceSysSoapClient(binding, endpoint);

            string str = ko.HelloWorld();
            MessageBox.Show(str);
        }
        /// <summary>
        /// Hello World 88
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTestHelloWorld2_Click(object sender, EventArgs e)
        {
            /*
            BasicHttpBinding binding = new BasicHttpBinding();
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
            binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;

            EndpointAddress endpoint =
                new EndpointAddress(@"http://sp.korablik.local:88/WebServiceCommon.asmx");

            KoReference88Common.WebServiceCommonSoapClient Ko =
                new KoReference88Common.WebServiceCommonSoapClient(binding, endpoint);

            //Ko.ClientCredentials.Windows.AllowedImpersonationLevel =
            //    System.Security.Principal.TokenImpersonationLevel.Impersonation;

            //Ko.ClientCredentials.Windows.ClientCredential.Domain = "";
            Ko.ClientCredentials.Windows.ClientCredential.UserName = "user";
            Ko.ClientCredentials.Windows.ClientCredential.Password = "password";

            Ko.ClientCredentials.UserName.UserName = "user";
            Ko.ClientCredentials.UserName.Password = "password";
            */


            //System.Net.CredentialCache myCredentials = 
            //    new System.Net.CredentialCache();
            //System.Net.NetworkCredential netCred =
            //    new System.Net.NetworkCredential("user", "password");
            //myCredentials.Add(new Uri(@"http://sp.korablik.local:88/WebServiceCommon.asmx")
            //    , "Basic", netCred);
            //

            //
            // Proxy Version
            //
            EndpointAddress endpoint =
                new EndpointAddress(@"http://sp.korablik.local:88/WebServiceCommon.asmx");

            BasicHttpBinding binding = new BasicHttpBinding();
            binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
            //binding.ProxyAddress = new Uri("http://proxy.local:8080");
            //binding.BypassProxyOnLocal = true;
            binding.UseDefaultWebProxy = true;

            binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Windows;
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

            KoReference88Common.WebServiceCommonSoapClient Ko =
                new KoReference88Common.WebServiceCommonSoapClient(binding, endpoint);

            Ko.ClientCredentials.Windows.ClientCredential.Domain = "DOMAIN";
            Ko.ClientCredentials.Windows.ClientCredential.UserName = "user";
            Ko.ClientCredentials.Windows.ClientCredential.Password = "pwd";

            Ko.ClientCredentials.UserName.UserName = "WebServiceUser";
            Ko.ClientCredentials.UserName.Password = "pwd";

            string str = Ko.HelloWorld();
            MessageBox.Show(str);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click_1(object sender, EventArgs e)
        {
            BasicHttpBinding binding = AServiceConfiguration.Binding;

            binding.Security.Mode = 
                BasicHttpSecurityMode.TransportCredentialOnly;
            binding.UseDefaultWebProxy = true;
            //binding.ProxyAddress =
            //    new Uri(KoEnvironment.GetAppSetting("ProxyAddress"));
            binding.BypassProxyOnLocal = false;
            binding.Security.Transport.ProxyCredentialType = 
                HttpProxyCredentialType.Windows;
            binding.Security.Transport.ClientCredentialType = 
                HttpClientCredentialType.Basic;
            
            var client = new KoReference88Common.WebServiceCommonSoapClient(
                binding, AServiceConfiguration.Endpoint);

            client.ClientCredentials.Windows.ClientCredential.Domain = 
                KoEnvironment.GetAppSetting("ProxyDomainName");
            client.ClientCredentials.Windows.ClientCredential.UserName = 
                KoEnvironment.GetAppSetting("ProxyUID");
            client.ClientCredentials.Windows.ClientCredential.Password =
                KoEnvironment.GetAppSetting("ProxyPwd");


            client.ClientCredentials.UserName.UserName = 
                KoEnvironment.GetAppSetting("WebServiceUser");
            client.ClientCredentials.UserName.Password = 
                KoEnvironment.GetAppSetting("WebServicePass");

            string str = client.HelloWorld();
            MessageBox.Show(str);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            DataTable tbl = new DataTable();            
            Exception err = KoPortal.Sys.Data.MySQL.DBKoLocal.GetMySqlSrvList_In88(ref tbl);
            // KoPortal.Sys.Data.MySQL.DBKoLocal.GetMySqlSrvList_In88(ref tbl, @"DOMAIN\WebServiceUID", @"pwd");            
            if (err == null)
                dg.DataSource = tbl;
            else
                MessageBox.Show(err.Message);
        }
        private void button3_Click(object sender, EventArgs e)
        {
            GetMySqlSrvList_ByIDTest();
        }
        public void GetMySqlSrvList_ByIDTest()
        {
            //DataTable parTbl = null;
            //int parID = 1;
            Exception actual = null;
            //actual = KoPortal2.Sys.Data.MySQL.DBKoLocal.GetMySqlSrvList_ByID(ref parTbl, parID);
            
            List<KoPortal2.Sys.Data.Axapta.Suplier> list = 
                new List<KoPortal2.Sys.Data.Axapta.Suplier>();
            actual = KoPortal2.Sys.Data.Axapta.DBSuplier.GetDataObj_ByID(ref list, "1205");

            int i = 0;
            i++;

        }
    }

    /// <summary>
    /// A code-based configuration for a WCF Service for use in Web Part code, 
    /// which isn't gangsta enough to have access to a .config file.
    /// </summary>
    public class AServiceConfiguration
    {
        /// <summary>
        /// Gets a configured binding for the service.
        /// </summary>
        public static BasicHttpBinding Binding
        {
            get
            {
                BasicHttpBinding _binding = new BasicHttpBinding()
                {
                    Name = "BasicHttpBinding_IAmAService",
                    CloseTimeout = new TimeSpan(0, 1, 0),
                    OpenTimeout = new TimeSpan(0, 1, 0),
                    ReceiveTimeout = new TimeSpan(0, 10, 0),
                    SendTimeout = new TimeSpan(0, 1, 0),
                    AllowCookies = false,
                    BypassProxyOnLocal = false,
                    HostNameComparisonMode = HostNameComparisonMode.StrongWildcard,
                    MaxBufferSize = 65536,
                    MaxBufferPoolSize = 524288,
                    MaxReceivedMessageSize = 65536,
                    MessageEncoding = WSMessageEncoding.Text,
                    TextEncoding = System.Text.Encoding.UTF8,
                    TransferMode = TransferMode.Buffered,
                    UseDefaultWebProxy = true,
                    ReaderQuotas = new XmlDictionaryReaderQuotas()
                    {
                        MaxDepth = 32,
                        MaxStringContentLength = 8192,
                        MaxArrayLength = 2097151,
                        MaxBytesPerRead = 4096,
                        MaxNameTableCharCount = 16384
                    }
                };

                return _binding;
            }
        }

        /// <summary>
        /// Gets the endpoint for the service.
        /// </summary>
        public static EndpointAddress Endpoint
        {
            get { return new EndpointAddress("http://sp.korablik.local:88/WebServiceCommon.asmx"); }
        }
    }

}


